package edu.xidian.sselab.cloudcourse.inputdata;

import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBaseCreateTable {

	public static  void CreateTable(String tableName, List<String> families) throws IOException {

		HbaseClient hc = new HbaseClient();
		Connection connection = hc.getConnection(); //建立连接
		Admin admin = connection.getAdmin(); //admin实例实现表操作
		TableName name = TableName.valueOf(tableName);  //创建表实例

		if (admin.tableExists(name)) {
			//如果需要建立的表已经存在
			System.out.println("此表已经存在！");
		} else {
			//如果需要建立的表不存在，新建表，插入列族
			HTableDescriptor tableDescriptor = new HTableDescriptor(name);
			for (String family : families) {
				tableDescriptor.addFamily(new HColumnDescriptor(family));
			}
				admin.createTable(tableDescriptor); //通过admin实例创建表
		}
		admin.close();
		connection.close();
		hc.closeConnection();
	}

	public static void main(String[] args) throws IOException {
		List<String> family = new ArrayList<>();
		family.add("info");
		HBaseCreateTable.CreateTable("create_test_table" , family);
	}
}
